<?php
if(!defined('IN_MANU')){
	die('[ERROR] You cannot load this page directly !!!');
}

Class DB{
	private $registry;

	private $userManager    	= null;
	private $roleManager    	= null;
	private $privilegeManager  	= null;
	private $profileManager     = null;
	private $configManager 		= null;
	public $connection;

	function __construct(){
		$argv = func_get_args();
		switch( func_num_args() )
		{
			default:
			case 0:
				self::__construct1();
			break;
			case 1:
				self::__construct1();
				self::__construct2($argv[0]);
				break;
		}

	}

	function __construct1(){
		$this->userManager      = new UserManager($this);
		$this->profileManager   = new ProfileManager($this);
		$this->roleManager      = new RoleManager($this);
		$this->privilegeManager = new PrivilegeManager($this);
		$this->configManager	= new ConfigManager($this);
	}

	function __construct2($registry){
		$this->registry = $registry;
	}

	/**
	 * get a (user, role, privilege, profile) Manager
	 */
	public function get($varName)
	{
		return $this->$varName;
	}

	/**
	 * load connection config from config.class.php
	 */
	public function getConnectionConfig()
	{
		//$cfg = new ConfigManager($this);

		return $this->configManager;
	}

	private function printDBError($string)
	{
		if(isset($this->registry) && $this->registry->debug){
			die($string);
		}
		else {
			die('[DB Error] There are some errors with the database ! Plz contact the <a href="ymsgr:sendim?hung.vjnh">administrator</a> to fix it !');
		}
	}

	/**
	 * start connection
	 */
	public function startConnection()
	{
		//$cfg = $this->getConnectionConfig();
		if (!$this->connection) {
		$this->connection = oci_connect("sys",/*pass*/ "admin", null, null, OCI_SYSDBA);
		}
		
		if (!$this->connection) {
			$m = oci_error();
			echo $m['message'], "\n";
		}
		else {
			//print "Connected to Oracle!";
		}
	}
	public function startConnectionWith($user, $pass)
	{
		//$cfg = $this->getConnectionConfig();
		if (!$this->connection) {
		$this->connection = oci_connect($user,$pass);
		}
		
		if (!$this->connection) {
			$m = oci_error();
			echo $m['message'], "\n";
		}
		else {
			//print "Connected to Oracle!";
		}
	}

	/**
	 * close connection
	 */
	public function closeConnection()
	{
		if($this->connection){
			oci_close($this->connection);
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Query function
	 *
	 * Used to call from entity model classes
	 *
	 * @param $query
	 */
	public function dbQuery($query)
	{
		if($this->connection){
			$stid = oci_parse($this->connection, $query);
			$handle = oci_execute($stid);
			if (!$handle) {
				$m = oci_error();
				$this->printDBError('Invalid query: ' . $m['message']. ' ' . $query);
				return false;
			}
			else {
				return $stid;
			}
		}
		else {
			return false;
		}
	}

	/**
	 * @description: get Instances
	 *
	 * @param:		 $dataType
	 */
	public function getClassName($dataType)
	{
		if($dataType == 'user'){
			return new User($this);
		}
		else if($dataType == 'role'){
			return new Role($this);
		}
		else if($dataType == 'privilege'){
			return new Privilege($this);
		}
		else if($dataType == 'profile'){
			return new Profile($this);
		}
		else return null;
	}


	/**
	 * query function
	 *
	 * @param SQL_Query $query
	 * @param string 	$datatype
	 */
	public function getData($query, $datatype){
		
		if(!$this->connection){
			return null;
		}

		$stid = $this->dbQuery($query); //get resources from db

		$objectArray = array(); //array of object for each result row

		if ($stid)
		{
			while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC))
			{
				$object = $this->getClassName($datatype);
					
				//set object atrribute				
				foreach ($row as $key => $value) {
					$object->set($key, $value);
				}
				
				array_push($objectArray, $object);
			}
		}
		else {
			return null;
		}
		
		return $objectArray;
	}	
}

?>